#
# Copyright (C) 2006-2008 X-Wrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=webif-lua
PKG_VERSION:=0.3
PKG_RELEASE:=$(shell LC_ALL=C svn info ${PKG_SOURCE_URL} | sed -ne's/^Last Changed Rev: //p')

PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/kernel.mk

define Package/webif-lua
  SECTION:=admin
  CATEGORY:=Administration
  SUBMENU:=webif-lua
  DEPENDS:=+haserl +opkg
  TITLE:=A HTTP administrative console for OpenWrt
  VERSION:=$(PKG_VERSION)-$(PKG_RELEASE)
endef

define Package/webif-lua/conffiles
/etc/config/webif
/etc/config/cron
/etc/config/syslog
endef

define Package/webif-lua-subpackage
  $(call Package/webif-lua)
  DEPENDS:=+webif-lua
  PKGARCH:=all
endef

define Package/webif-lua-lang-ca
  $(call Package/webif-lua-subpackage)
  TITLE:=Catalan language file for webif-lua^2
endef

define Package/webif-lua-lang-cs
  $(call Package/webif-lua-subpackage)
  TITLE:=Czech language file for webif-lua^2
endef

define Package/webif-lua-lang-da
  $(call Package/webif-lua-subpackage)
  TITLE:=Danish language file for webif-lua^2
endef

define Package/webif-lua-lang-de
  $(call Package/webif-lua-subpackage)
  TITLE:=German language file for webif-lua^2
endef

define Package/webif-lua-lang-ee
  $(call Package/webif-lua-subpackage)
  TITLE:=Estonian language file for webif-lua^2
endef

define Package/webif-lua-lang-es
  $(call Package/webif-lua-subpackage)
  TITLE:=Spanish language file for webif-lua^2
endef

define Package/webif-lua-lang-fr
  $(call Package/webif-lua-subpackage)
  TITLE:=French language file for webif-lua^2
endef

define Package/webif-lua-lang-hr
  $(call Package/webif-lua-subpackage)
  TITLE:=Croatian language file for webif-lua^2
endef

define Package/webif-lua-lang-hu
  $(call Package/webif-lua-subpackage)
  TITLE:=Hungarian language file for webif-lua^2
endef

define Package/webif-lua-lang-it
  $(call Package/webif-lua-subpackage)
  TITLE:=Italian language file for webif-lua^2
endef

define Package/webif-lua-lang-nl
  $(call Package/webif-lua-subpackage)
  TITLE:=Dutch language file for webif-lua^2
endef

define Package/webif-lua-lang-no
  $(call Package/webif-lua-subpackage)
  TITLE:=Norwegian language file for webif-lua^2
endef

define Package/webif-lua-lang-pl
  $(call Package/webif-lua-subpackage)
  TITLE:=Polish language file for webif-lua^2
endef

define Package/webif-lua-lang-pt
  $(call Package/webif-lua-subpackage)
  TITLE:=Portuguese language file for webif-lua^2
endef

define Package/webif-lua-lang-ru
  $(call Package/webif-lua-subpackage)
  TITLE:=Russian language file for webif-lua^2
endef

define Package/webif-lua-lang-sq
  $(call Package/webif-lua-subpackage)
  TITLE:=Albanian language file for webif-lua^2
endef

define Package/webif-lua-lang-sv
  $(call Package/webif-lua-subpackage)
  TITLE:=Swedish language file for webif-lua^2
endef

define Package/webif-lua-lang-uk
  $(call Package/webif-lua-subpackage)
  TITLE:=Ukrainian language file for webif-lua^2
endef

define Package/webif-lua-lang-zh
  $(call Package/webif-lua-subpackage)
  TITLE:=Chinese language file for webif-lua^2
endef

define Package/webif-lua-theme-zephyr
  $(call Package/webif-lua-subpackage)
  TITLE:=Zephyr theme for webif-lua^2
endef

define Package/webif-lua-theme-xwrt
  $(call Package/webif-lua-subpackage)
  TITLE:=Original theme for webif-lua^2
endef

define Package/webif-lua-theme-xwrt-mini
  $(call Package/webif-lua-subpackage)
  TITLE:=Variant of the original theme (blank header)
endef

define Package/webif-lua-theme-sedky1
  $(call Package/webif-lua-subpackage)
  TITLE:=Sedky theme for webif-lua^2
endef

define Package/webif-lua-theme-clubman
  $(call Package/webif-lua-subpackage)
  TITLE:=Clubman theme for webif-lua^2
endef

define Build/Prepare
	$(call Build/Prepare/Default)
	$(CP) src/* $(PKG_BUILD_DIR)/
endef

define Build/Configure
endef

define Build/Compile
	$(TARGET_CC) $(TARGET_CFLAGS) \
		-D_METAPACK \
		-I$(STAGING_DIR)/include-host -I$(STAGING_DIR_HOST)/include -include endian.h \
		$(TARGET_CFLAGS_EXTRA) \
		$(TARGET_LDFLAGS) \
		-o $(PKG_BUILD_DIR)/webifmetabin \
		src/int2human/main.c src/int2human/human_readable.c \
		src/wepkeygen/keygen.c src/wepkeygen/md5.c \
		src/webif-page.c src/bstrip.c src/webifmetabin.c
	$(STRIP) $(PKG_BUILD_DIR)/webifmetabin
	./create_lang_list.sh . $(PKG_BUILD_DIR)
endef

define Package/webif-lua/install
	$(CP) -a ./files/* $(1)/
	$(INSTALL_DATA) $(PKG_BUILD_DIR)/languages.root $(1)/etc/
	$(INSTALL_BIN) ./ipkg/webif.preinst $(1)/CONTROL/preinst
	$(INSTALL_BIN) ./ipkg/webif.postinst $(1)/CONTROL/postinst
	$(SED) 's,$$$$S,$(BOARD),g' $(1)/CONTROL/postinst
	chmod -R 755 $(1)/*
	rm -rf \
		$(1)/usr/lib/webif/lang/ \
		$(1)/www/themes/zephyr/ \
		$(1)/www/themes/xwrt-mini/ \
		$(1)/www/themes/sedky1/ \
		$(1)/www/themes/xwrt/
	./embed_rev.sh $(1)
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/webifmetabin $(1)/usr/bin/webifmetabin
	find $(1) -name 'CVS' -o -name '.svn' -o -name '*~' | $(XARGS) rm -rf
endef

define BuildLang
  define Package/webif-lua-lang-$(1)/install
	$(INSTALL_DIR) $$(1)/usr/lib/webif/lang/$(1)/
	$(INSTALL_DATA) ./files/usr/lib/webif/lang/$(1)/common.txt \
		$$(1)/usr/lib/webif/lang/$(1)/
  endef

  $$(eval $$(call BuildPackage,webif-lua-lang-$(1)))
endef

define BuildTheme
  define Package/webif-lua-theme-$(1)/install
	$(INSTALL_DIR) $$(1)/www/themes/$(1)/
	$(INSTALL_DATA) ./files/www/themes/$(1)/* \
		$$(1)/www/themes/$(1)/
  endef

  $$(eval $$(call BuildPackage,webif-lua-theme-$(1)))
endef

$(eval $(call BuildPackage,webif-lua))
$(eval $(call BuildLang,ca))
$(eval $(call BuildLang,cs))
$(eval $(call BuildLang,da))
$(eval $(call BuildLang,de))
$(eval $(call BuildLang,ee))
$(eval $(call BuildLang,es))
$(eval $(call BuildLang,fr))
$(eval $(call BuildLang,hr))
$(eval $(call BuildLang,hu))
$(eval $(call BuildLang,it))
$(eval $(call BuildLang,nl))
$(eval $(call BuildLang,no))
$(eval $(call BuildLang,pl))
$(eval $(call BuildLang,pt))
$(eval $(call BuildLang,ru))
$(eval $(call BuildLang,sq))
$(eval $(call BuildLang,sv))
$(eval $(call BuildLang,uk))
$(eval $(call BuildLang,zh))
$(eval $(call BuildTheme,zephyr))
$(eval $(call BuildTheme,xwrt))
$(eval $(call BuildTheme,xwrt-mini))
$(eval $(call BuildTheme,sedky1))
$(eval $(call BuildTheme,clubman))
